package cn.pegasus.teamwork.pegasus.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.iyizhan.teamwork.base.util.PageHelper;
import cn.iyizhan.teamwork.base.util.PageObject;
import cn.pegasus.teamwork.pegasus.entity.BussWasherFunddetails;
import cn.pegasus.teamwork.pegasus.entity.BussWasherFundstatistics;
import cn.pegasus.teamwork.pegasus.mapper.BussServiceProjectMapper;
import cn.pegasus.teamwork.pegasus.mapper.BussWasherFunddetailsMapper;
import cn.pegasus.teamwork.pegasus.mapper.BussWasherFundstatisticsMapper;
import cn.pegasus.teamwork.pegasus.service.BussServiceProjectService;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;
import cn.pegasus.teamwork.pegasus.service.BussWasherFunddetailsService;
import cn.pegasus.teamwork.util.DateCompareUtil;
import cn.pegasus.teamwork.vo.WasherOrderVo;

/**
 * @author ryan.bian
 * @version 
 */
@Service("bussWasherFunddetailsService")
public class BussWasherFunddetailsServiceImpl extends BaseServiceImpl<BussWasherFunddetails, BussWasherFunddetailsMapper> implements BussWasherFunddetailsService {
	// 注入当前dao对象
    @Autowired
    private BussWasherFunddetailsMapper bussWasherFunddetailsMapper;
    @Autowired
	private BussUcenterAreaService ucenterAreaService;
    @Autowired
	private BussServiceProjectService serviceProjectService;
    @Autowired
    private BussWasherFundstatisticsMapper washerFundstatisticsMapper;

    public BussWasherFunddetailsServiceImpl() {
        setMapperClass(BussWasherFunddetailsMapper.class, BussWasherFunddetails.class);
    }

	@Override
	public PageObject<BussWasherFunddetails> PageList(Map<String, Object> map) {
		int totalData = bussWasherFunddetailsMapper.getTotal(map);
        PageHelper pageHelper = new PageHelper(totalData, map);
        List<BussWasherFunddetails> list = bussWasherFunddetailsMapper.findListByPage(pageHelper.getMap());
        PageObject<BussWasherFunddetails> pageObject = pageHelper.getPageObject();
        pageObject.setDataList(list);
		return pageObject;
	}
	
	
	@Override
	public void updateStatus(Long id, Integer ispaymoney, Integer examineStatus) {
		BussWasherFunddetails tail = new BussWasherFunddetails();
		tail.setId(id);
		if(null != ispaymoney){
			tail.setIspaymoney(ispaymoney);
		}
		if(null != examineStatus){
			if(examineStatus == 2){
				tail.setExamineStatus(examineStatus);
				tail.setIspaymoney(2);
			}else{
				tail.setExamineStatus(examineStatus);
			}
		}
		bussWasherFunddetailsMapper.updateByEntity(tail);
	}

	@Override
	public void updateDeposit(Integer id) {
		BussWasherFundstatistics w = new BussWasherFundstatistics();
		w.setId(Long.valueOf(id.toString()));
		w = washerFundstatisticsMapper.selectSingle(w);
		//生成洗车工流水
		BussWasherFunddetails f = new BussWasherFunddetails();
		f.setWasherId(id);
		f.setCapitalType(11);
		f.setTransactionTime(new Date());
		f.setCapitalBalance(w.getWasherDeposit());
		f.setCreateTime(new Date());
		f.setStatus(1);
		bussWasherFunddetailsMapper.insertByEntity(f);
		//修改账户资金
		BussWasherFundstatistics s = new BussWasherFundstatistics();
		s.setWasherDeposit(0l);
		s.setId(Long.valueOf(id.toString()));
		washerFundstatisticsMapper.updateByEntity(s);
	}

	@Override
	public PageObject<BussWasherFunddetails> PageWasher(Map<String, Object> map) {
		int totalData = bussWasherFunddetailsMapper.getTotals(map);
        PageHelper pageHelper = new PageHelper(totalData, map);
        List<BussWasherFunddetails> list = bussWasherFunddetailsMapper.findListByWaher(pageHelper.getMap());
        PageObject<BussWasherFunddetails> pageObject = pageHelper.getPageObject();
        pageObject.setDataList(list);
		return pageObject;
	}

	@Override
	public List<WasherOrderVo> findWasherOrderVo(Long id) throws Exception{
		List<WasherOrderVo> list = new ArrayList<WasherOrderVo>();
		//查询提款基本信息
		BussWasherFunddetails t = new BussWasherFunddetails();
		t.setId(id);
		BussWasherFunddetails bt = bussWasherFunddetailsMapper.selectSingle(t);
		//查询最近时间的金额，如果等于现在的就用时间去查询
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("washerId", bt.getWasherId());
		long money = bt.getCapitalBalance();
		Date startTime = DateCompareUtil.getMonthStart(new Date());
		boolean flag = false;
		do{
			String time = DateCompareUtil.getStrDate(startTime,"yyyy-MM-dd HH:mm:ss");
			params.put("startTime", time);
			Long lastMomey = bussWasherFunddetailsMapper.getMoney(params);
			if(null != lastMomey && money <= lastMomey){
				flag = true;
			}else{
				startTime = DateCompareUtil.lastMonthDate(startTime);
			}
		}while(!flag);
		
		//先查询洗车的
		list = bussWasherFunddetailsMapper.findWasherOrderVo(params);
		if(null != list && list.size() > 0){
			for(WasherOrderVo vo : list){
				vo.setCapitalName("洗车结款");
				if(vo.getRechargeType()==1){
					vo.setRecharge("微信支付");
				}
				if(vo.getRechargeType()==2){
					vo.setRecharge("余额付款");
				}
				// 获取区域
				Integer areaId = vo.getAreaId();
				if (null != areaId) {
					String name = ucenterAreaService.getAreaName(areaId);
					vo.setAreaName(name);
				}
				// 服务项目
				if (vo.getProjectId() != null) {
					String projectName = serviceProjectService.findProjectName(Long.valueOf(vo.getProjectId()));
					vo.setProjectName(projectName);
				}
				
			}
		}
		//查询打赏的
		List<WasherOrderVo> volist = bussWasherFunddetailsMapper.findRewardOrder(params);
		if(null != volist && volist.size() > 0){
			for(WasherOrderVo vo : volist){
				vo.setCapitalName("打赏结款");
				if(vo.getRechargeType()==1){
					vo.setRecharge("微信支付");
				}
				if(vo.getRechargeType()==2){
					vo.setRecharge("余额付款");
				}
				list.add(vo);
			}
		}
		return list;
	}
}
